﻿using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.WebAPI.Models;

namespace ThresholdAssistant.WebAPI.Repository
{
    /// <summary>
    /// 二维码生成记录表仓储
    /// </summary>
    public class CreateQrcodeRecordRepository : BaseRepository, IBaseAutofac
    {
        /// <summary>
        /// 新增
        /// </summary>
        public async Task<bool> AddAsync(CreateQrcodeRecord model)
        {
            string insertSql = @"insert into `t_create_qrcode_record`
                                    (`member_id`,`local_file_path`,`data`,`create_time`,`update_time`,`remark`)
                                values
                                    (@MemberId,@LocalFilePath,@Data,@CreateTime,@UpdateTime,@Remark)";
            return await mydb.ExecuteAsync(insertSql, model);
        }

        /// <summary>
        /// 查询
        /// </summary>
        public async Task<CreateQrcodeRecord> GetInfoByMemberIdAsync(long memberId)
        {
            string querySql = @"select
                                `id`, `member_id`, `local_file_path`, `data`, `create_time`, `update_time`, `remark`
                             from `t_create_qrcode_record`
                             where member_id = @MemberId limit 1;";
            return await mydb.GetFirstOrDefaultAsync<CreateQrcodeRecord>(querySql, new { MemberId = memberId });
        }

        /// <summary>
        /// 邀请码反查
        /// </summary>
        public async Task<CreateQrcodeRecord> GetInfoByDataAsync(string data)
        {
            string querySql = @"select
                                `id`, `member_id`, `local_file_path`, `data`, `create_time`, `update_time`, `remark`
                             from `t_create_qrcode_record`
                             where data = @Data limit 1;";
            return await mydb.GetFirstOrDefaultAsync<CreateQrcodeRecord>(querySql, new { Data = data });
        }
    }
}
